package engine.control.loading
{
	import com.greensock.TweenLite;
	
	import engine.logic.loading.LoadingLogic;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	import flash.utils.flash_proxy;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;

	public class NxLoading2 extends Sprite
	{
		private var _totalWidth:Number;
		private var _totalX:Number;
		private var _curProcess:Number;
		private var _f5Txt:TextField;
		private var _prompTxt:TextField;
		
		//用来屏蔽的显示对象
		private var _shieldDS:Sprite;
		private var _loadingBar:MovieClip;
		
		private var _titleTxt:TextField;
		private var _titlePrecent:TextField;
		private var _loadingLine:DisplayObject;
		/**背景图片容器**/
		private var _backMcGroup:MovieClip;
		/**背景图片容器数组**/
		private  var _backMcArr:Array;
		
		private var _width		: int;
		private var _height	: int; 
		
		private var _timer:Timer;
		private var _promptNum:int	=	0;
		private var _promptLength:int;
		private var _tempVal:int;
		public function NxLoading2(skin:MovieClip)
		{
			_loadingBar=skin;
			init();
		}
		
		private function init():void
		{
			_backMcGroup = new MovieClip();
			_loadingBar.addChildAt(_backMcGroup,0);
			_shieldDS=new Sprite();
			
			_width = 1280;
			_height= 750;
			
			var txtStyle : StyleSheet = new StyleSheet();
			txtStyle.setStyle("a:link"		,{color:"#FFFF00",textDecoration:"underline"});
			txtStyle.setStyle("a:visited"	,{color:"#FFFF00"});
			txtStyle.setStyle("a:hover"	,{color:"#FFFF00",textDecoration:"none"});
			
			_titleTxt=_loadingBar.process_bar.titleTxt;
			_loadingLine=_loadingBar.process_bar.loadingLine;
			_titlePrecent=_loadingBar.process_bar.txt_3;
			_f5Txt=_loadingBar.process_bar.txt_4;
			_f5Txt.htmlText = "<a href='event:f5'>F5</a>" ;
			_f5Txt.styleSheet = txtStyle;
			_f5Txt.addEventListener( TextEvent.LINK, onLink );
			_totalX=_loadingLine.x;
			_totalWidth =_loadingLine.width;
			
			_shieldDS.graphics.beginFill(0x000000);
			_shieldDS.graphics.drawRect(0,0,100,100);
			_shieldDS.graphics.endFill();
			
			_titleTxt.autoSize=TextFieldAutoSize.LEFT;
			
			addChild(_shieldDS);
			addChild(_loadingBar);
			_timer	=	new Timer(1000);
			_timer.addEventListener(TimerEvent.TIMER, timerHandle);
		}
		
		private function onLink(evt : TextEvent):void
		{
			navigateToURL( new URLRequest("javascript:window.location.reload(false);"), "_self" );
		}
		
		public function onChangeScene(w:Number,h:Number,x:int,y:int):void
		{
			_shieldDS.width		=	w;
			_shieldDS.height	=	h;
			_shieldDS.x			=	x;
			_shieldDS.y			=	y;
			_loadingBar.x		=	x	+	(	_shieldDS.width	-	_width)	/	2;
			_loadingBar.y		=	y 	+	(	_shieldDS.height-	_height)/	2;
			
			var stage : Stage = StageInfo.stage;
			try
			{
				if( stage != null )
				{
					var point : Point = parent.globalToLocal( new Point( ( stage.stageWidth - w ) * .5,
						( stage.stageHeight - h ) * .5 ) );
					x = point.x;
					y = point.y;
				}
			} 
			catch(error:Error) 
			{
			}
		}
		
		public function setProcess(proc:Number,titleStr:String=""):void
		{
			if(proc==0)
			{
				if(this.parent==null)
				{
//					clearBg();
//					randomBg();		
					var intervalTime:int	=	int(StaticIni.getIniVar("Change_time","time"))*1000;
					// _promptLength			=	int(StaticIni.getIniVar("Tips_total","num"))
					_timer.reset();
					_timer.delay	=	intervalTime;
					_timer.start();
					timerHandle();
//					timerHandle();
					StageInfo.systemSpace.addChild(this);
				}
			}
			LoadingLogic.getInstance().onChangeScene();
			_curProcess=proc;
			
			_loadingLine.x=_totalX + _totalWidth*_curProcess/100;
			_titlePrecent.text	  =	_curProcess+"%";
			if(	_titleTxt.text	!=	titleStr)
			{
				_titleTxt.text=titleStr;
			}
			
			
			if(proc==100)
			{
				TweenLite.to(_loadingLine,.1,{width:_totalWidth,onComplete:removeFromStage});
			}
		}
		
		private function removeFromStage():void	
		{
			_timer.stop();
			if(_curProcess==100)
			{
				if(this.parent)this.parent.removeChild(this);
			}
			else
			{
				_loadingLine.x=_totalX;
			}
		}
		
		public function hide():void
		{
//			if(this.parent)		
//			{
//				this.parent.removeChild(this);
//			}
		}
		
		public function shieldArea(w:Number,h:Number):void
		{
			_shieldDS.width=w;
			_shieldDS.height=h;
			_loadingBar.x=x;
			_loadingBar.y=y;
		}
		/**
		 *得到背景图片 
		 */	
		public  function  complete(val:MovieClip,pList:Array):void
		{		
//			clearBg();
//			_backMcGroup.addChild(val);
		}
		
		private function randomBg():void
		{
			if( _backMcArr == null )
			{
				var obj:Object	=	StaticIni.getIniObj("backMcRes");
				_backMcArr	=	(obj["backMcResLoad"] as String).split(",");
			}
			
			var url:String	=	_backMcArr[int(_backMcArr.length*Math.random())];
			ResManager.loadRes(url, url, complete );
		}
		
		private function clearBg():void
		{
			while(_backMcGroup.numChildren)
			{
				_backMcGroup.removeChildAt( 0 );
			}
		}
		
		/**
		 * 小提示循环显示 
		 * @param e
		 * 
		 */		
		private function timerHandle(e:TimerEvent=null):void
		{
			_promptLength	=	int(StaticIni.getIniVar("Tips_total","num"))
				
			_promptNum		=	Math.floor(Math.random() * _promptLength);
			//处理两次随到相同数
			if(_promptNum == _tempVal)
			{
				_promptNum	=	_tempVal	=	_promptNum + 1 ;
			}
			else
			{
				_tempVal	=	_promptNum
			}
			if(_promptNum >= _promptLength)_promptNum = 0;
//			trace(_promptNum+"asjdjasjdjasjd_promptNum");
			var str:String	=	StaticIni.getIniVar("Tips_"+_promptNum,"content");
			var promptStr:String	=	StaticIni.getSwfLangStr(str);
			_prompTxt				=	_loadingBar.process_bar.txt_5;
			_prompTxt.htmlText		=	"";
			_prompTxt.htmlText		=	promptStr;
		}

	}
}